package evaluate;

import java.io.IOException;
import java.util.Random;

import common_var.Path;

import benchmark_lucene.SearchLucene;

public class MaximinMAP {
	public int times = 20;
	public float MaxMap = 0.0f;
	public float MaxR_title = 0.0f;
	public float GetMaximinMap() throws IOException {
		SearchLucene sl = new SearchLucene(Path.current_index_path);
		float last = 0f;
		float current = 0f;
		float r_title = 0.05f;
		float r_dt = 0.1f;
		float skip_dt = 1.0f;
		boolean flag = true;
		for (int i = 0; i < this.times; i++) {			
			current = sl.TestBed(r_title, Path.ret_control);// Entry
			System.out.println("Current MAP Is: " + current);
			if (current > this.MaxMap)
			{
				this.MaxMap = current;
				this.MaxR_title = r_title;
			}
			
			Random rd = new Random();
			if(rd.nextFloat()>0.8f)//20 % chance to skip;
			{
				if(rd.nextFloat()>0.5)
				{
					r_title+=skip_dt;
				}
				else
				{
					r_title-=skip_dt;
				}
				continue;
			}
			
			if (flag) {
				if (current - last >= 0) {
					r_title += r_dt;
				}

				else {
					r_title -= r_dt;
					flag = false;
				}
			} else {
				if (current - last >= 0) {
					r_title -= r_dt;
				} else {
					r_title += r_dt;
					flag = true;
				}
			}
			
		}
		return this.MaxR_title;
		// TODO Auto-generated constructor stub
	}

	/**
	 * @param args
	 * @throws IOException
	 */
	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
		Path.Init(0);
		MaximinMAP mm = new MaximinMAP();
		System.out.println("Best R_Title Is: " + mm.GetMaximinMap()
				+ "\nMax MAP Is: " + mm.MaxMap);
	}

}
